<template>
  <div class="app-container">

    <!--表格渲染-->

    <div class="title-btns">
      <span class="role-span"><span></span>岗位列表</span>
      <div style="float: right">
        <!-- 新增 -->
        <el-button v-permission="['ADMIN','USERJOB_ALL','USERJOB_CREATE']" class="filter-item" size="mini" type="primary" icon="el-icon-plus" @click="toAdd">新增</el-button>
        <el-button v-permission="['ADMIN','USERJOB_ALL','USERJOB_EDIT']" class="filter-item" size="mini" :disabled="multipleSelection.length !== 1" icon="el-icon-edit" type="primary" @click="toUpdate()">修改</el-button>
        <!-- 导出 -->
        <!-- <el-button v-permission="['ADMIN','USERJOB_ALL','USERJOB_SELECT']" :loading="downloadLoading" size="mini" class="filter-item" type="warning" icon="el-icon-download" @click="exportExcel">导出</el-button> -->
      </div>
    </div>
    <div class="head-container">
      <!-- 搜索 -->
      <div class="head-search">
        <el-input v-model="query.value" clearable placeholder="输入岗位名称搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="toQuery" />
        <el-select v-model="query.status" clearable placeholder="状态" class="filter-item" style="width: 90px" @change="toQuery">
          <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-button class="filter-item" size="mini" type="primary" icon="el-icon-search" @click="toQuery">搜索</el-button>
        <el-button class="filter-item" size="mini" icon="el-icon-refresh" @click="toReSet">重置</el-button>

      </div>
      <el-table v-loading="loading" :data="data" size="small" style="width: 100%;" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
          align="center"
        />
        <el-table-column prop="name" label="名称" />

        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <div v-for="item in dicts" :key="item.id">
              <template v-if="scope.row.status.toString() === item.value">{{ item.label }}</template>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建日期">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序">
          <template slot-scope="scope">
            {{ scope.row.sort }}
          </template>
        </el-table-column>
        <el-table-column v-if="checkPermission(['ADMIN','USERJOB_ALL','USERJOB_EDIT','USERJOB_DELETE'])" label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <el-button v-permission="['ADMIN','USERJOB_ALL','USERJOB_EDIT']" size="mini" type="text" @click="toUpdate(scope.row)">修改</el-button>
            <el-popover
              :ref="scope.row.id"
              v-permission="['ADMIN','USERJOB_ALL','USERJOB_DELETE']"
              placement="top"
              width="180"
            >
              <p>确定删除本条数据吗？</p>
              <div style="text-align: right; margin: 0">
                <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
                <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定</el-button>
              </div>
              <el-button slot="reference" type="text" style="margin-left:10px" size="mini">删除</el-button>
            </el-popover>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :total="total"
        :current-page="page"
        style="margin-top: 8px;"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="sizeChange"
        @current-change="pageChange"
      />
    </div>
    <eForm ref="form" :is-add="isAdd" :dicts="dicts" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import initDict from '@/mixins/initDict'
import { del } from '@/api/system/job'
import { parseTime } from '@/utils/index'
import eForm from './module/form'
export default {
  components: { eForm },
  mixins: [initData, initDict],
  data() {
    return {
      multipleSelection: [],
      delLoading: false,
      downloadLoading: false,
      enabledTypeOptions: [
        { key: '0', display_name: '正常' },
        { key: '1', display_name: '停用' }
      ]
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
      // 加载数据字典
      this.getDict('status')
    })
  },
  methods: {
    parseTime,
    checkPermission,
    beforeInit() {
      this.url = 'system/job'
      const sort = 'asc'
      const sortColumn = 'sort'
      this.params = { page: this.page, size: this.size, sortColumn: sortColumn, sort: sort }
      const query = this.query
      const value = query.value
      const status = query.status
      if (value) { this.params['name'] = value }
      if (status !== '' && status !== null) { this.params['status'] = status }
      return true
    },

    handleSelectionChange(val) {
      this.multipleSelection = val
      console.info(this.multipleSelection)
    },
    toAdd() {
      this.isAdd = true
      this.$refs.form.resetForm()
      this.$refs.form.dialog = true
    },
    toUpdate(data) {
      this.isAdd = false
      if (!data) {
        data = this.multipleSelection[0]
      }
      const _this = this.$refs.form
      _this.resetForm()
      _this.form = {
        id: data.id,
        name: data.name,
        sort: data.sort,
        status: data.status.toString()
      }
      _this.dialog = true
    },
    /**
     * 去删除
     */
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    // 导出文件
    exportExcel() {
      // exportExcel(this.params).then(response => {
      //   const filename = '用户数据.xls'
      //   if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      //     window.navigator.msSaveOrOpenBlob(response, filename)
      //   } else {
      //     var link = document.createElement('a')
      //     var blob = new Blob([response])
      //     link.download = filename
      //     link.style.display = 'none'
      //     link.href = URL.createObjectURL(blob)
      //     document.body.appendChild(link)
      //     link.click() // 执行下载
      //     URL.revokeObjectURL(link.href) // 释放url
      //     document.body.removeChild(link) // 释放标签
      //   }
      // })
    }
  }
}
</script>

<style scoped>

</style>
